﻿using System;
using UnityEngine;

namespace SuperScrollView
{
    public enum LoadingTipStatus
    {
        None,
        WaitContinureDrag,
        WaitRelease,
        WaitLoad,
        Loaded,
    }

    public class PullLoadMoreListView : SuperListView
    {

        public delegate void OnReqMoreData(Action callBack);

        LoadingTipStatus mLoadingTipStatus = LoadingTipStatus.None;

        OnReqMoreData OnGetMoreData = null;

        float mLoadingTipItemHeight = 100;

        public void InitMore(SuperListViewItem.OnSelectDelegate selctItemFunc, SuperListViewItem.OnShowDelegate showItemFunc, OnReqMoreData onGetMoreData)
        {
            Init(selctItemFunc, showItemFunc);
            OnGetMoreData = onGetMoreData;
        }

        // Use this for initialization
        void Start()
        {
            mLoopListView.mOnBeginDragAction = OnBeginDrag;
            mLoopListView.mOnDragingAction = OnDraging;
            mLoopListView.mOnEndDragAction = OnEndDrag;
        }
        override public LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
        {
            if (index < 0)
            {
                return null;
            }

            LoopListViewItem2 item = null;
            if (index == mDataCount)
            {
                item = listView.NewListViewItem("LoadWaitItem");
                UpdateLoadingTip(item);
                return item;
            }

            item = listView.NewListViewItem(mItemPrefabName);
            SuperListViewItem itemScript = item.GetComponent<SuperListViewItem>();
            if (item.IsInitHandlerCalled == false)
            {
                item.IsInitHandlerCalled = true;
                itemScript.Init();
                itemScript.OnSelectItem = OnSelctItem;
                itemScript.OnShowItem = OnShowItem;
            }
            if (index == mDataCount - 1)
            {
                item.Padding = 0;
            }
            itemScript.SetItemData(index);
            return item;
        }
        override public void SetCount(int count, bool resetPos = false, int moveIndex = -1)
        {
            mDataCount = count;
            mLoopListView.SetListItemCount(mDataCount + 1, resetPos, moveIndex);
        }

        public void SetDataCount(int count)
        {
            mDataCount = count;
        }

        void UpdateLoadingTip(LoopListViewItem2 item)
        {
            if (item == null)
            {
                return;
            }
            ListWaitItem itemScript0 = item.GetComponent<ListWaitItem>();
            if (itemScript0 == null)
            {
                return;
            }
            if (mLoadingTipStatus == LoadingTipStatus.None)
            {
                itemScript0.mRoot.SetActive(false);
                item.CachedRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
            }
            else if (mLoadingTipStatus == LoadingTipStatus.WaitRelease)
            {
                itemScript0.mRoot.SetActive(true);
                itemScript0.mText.text = "Release to Load More";
                itemScript0.mArrow.SetActive(true);
                itemScript0.mWaitingIcon.SetActive(false);
                item.CachedRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, mLoadingTipItemHeight);
            }
            else if (mLoadingTipStatus == LoadingTipStatus.WaitLoad)
            {
                itemScript0.mRoot.SetActive(true);
                itemScript0.mArrow.SetActive(false);
                itemScript0.mWaitingIcon.SetActive(true);
                itemScript0.mText.text = "Loading ...";
                item.CachedRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, mLoadingTipItemHeight);
            }
        }

        void OnBeginDrag()
        {

        }
        void OnDraging()
        {
            if (mLoopListView.ShownItemCount == 0)
            {
                return;
            }
            if (mLoadingTipStatus != LoadingTipStatus.None && mLoadingTipStatus != LoadingTipStatus.WaitRelease)
            {
                return;
            }
            LoopListViewItem2 item = mLoopListView.GetShownItemByItemIndex(mDataCount);
            if (item == null)
            {
                return;
            }
            LoopListViewItem2 item1 = mLoopListView.GetShownItemByItemIndex(mDataCount - 1);
            if (item1 == null)
            {
                return;
            }
            float y = mLoopListView.GetItemCornerPosInViewPort(item1, ItemCornerEnum.LeftBottom).y;
            if (y + mLoopListView.ViewPortSize >= mLoadingTipItemHeight)
            {
                if (mLoadingTipStatus != LoadingTipStatus.None)
                {
                    return;
                }
                mLoadingTipStatus = LoadingTipStatus.WaitRelease;
                UpdateLoadingTip(item);
            }
            else
            {
                if (mLoadingTipStatus != LoadingTipStatus.WaitRelease)
                {
                    return;
                }
                mLoadingTipStatus = LoadingTipStatus.None;
                UpdateLoadingTip(item);
            }
        }
        void OnEndDrag()
        {
            if (mLoopListView.ShownItemCount == 0)
            {
                return;
            }
            if (mLoadingTipStatus != LoadingTipStatus.None && mLoadingTipStatus != LoadingTipStatus.WaitRelease)
            {
                return;
            }
            LoopListViewItem2 item = mLoopListView.GetShownItemByItemIndex(mDataCount);
            if (item == null)
            {
                return;
            }
            mLoopListView.OnItemSizeChanged(item.ItemIndex);
            if (mLoadingTipStatus != LoadingTipStatus.WaitRelease)
            {
                return;
            }
            mLoadingTipStatus = LoadingTipStatus.WaitLoad;
            UpdateLoadingTip(item);
            DoReqMoreData();
        }

        void DoReqMoreData()
        {
            OnGetMoreData?.Invoke(OnDataSourceLoadMoreFinished);
        }

        void OnDataSourceLoadMoreFinished()
        {
            if (mLoopListView.ShownItemCount == 0)
            {
                return;
            }
            if (mLoadingTipStatus == LoadingTipStatus.WaitLoad)
            {
                mLoadingTipStatus = LoadingTipStatus.None;
                mLoopListView.SetListItemCount(mDataCount + 1, false);
                mLoopListView.RefreshAllShownItem();
            }
        }
    }

}

